package mobi.zeedev.flyingsaucer.common.DataModule;

import android.content.ContentValues;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
//import android.util.Log;

public class MetaBeer {
	
	

	public static final String TAG = "MetaBeer";

	public static final int COUNT = 25;

	public static final String BEER_ID = "beerId";
	public static final String COUNTRY_ID = "countryId";
	public static final String BREWER_ID = "brewerId";
	public static final String STYLE_ID = "styleId";
	
	public static final String RAW_NAME = "rawName";
	//child	
	public static final String NAME = "name";
	public static final String CAN = "can";
	public static final String NAME_SUBSCRIPT = "nameSubscript";
	
	public static final String CONTAINER = "container";
	//child
	public static final String BOTTLE = "draught";
	public static final String DRAUGHT = "bottle";
	
	public static final String DESCRIPTION = "description";
	public static final String CITY = "city";
	public static final String COUNTRY_NAME = "countryName";
	public static final String STYLE_NAME = "styleName";
	public static final String BREWER_NAME = "brewerName";
	public static final String AVERAGE_RATING = "averageRating";

	//DB Layered
	
	//Modified Externally only
	public static final String INTERNAL_DATE_ADDED = "internalDateAdded";
	public static final String DB_ID = "_id";
	public static final String STATUS = "status";
	
	//Internally Modified
	public static final String COMMENTS = "comments";
	public static final String RATING = "rating";

	public static final String DATE_ADDED = "dateAdded";
	//child
	//public static final String DATE_ADDED_RAW = "dateAddedRaw";
	public static final String SIMPLE_DATE = "simpleDate";	
	public static final String TASTED = "tasted";
	
	public static final String MEMBER_ID = "memberId"; 


	//NON-COUNTED, VIRTUAL PROPERTIES
	public static final String SIMPLE_INTERNAL_DATE = "simpleInternalDate"; 
	public static final String NEW_SIMPLE_DATE = "newSimpleDate";
	public static final String SIMPLE_INTERNAL_DATE_2 = "simpleInternalDate2";
	public static final String DESCRIPTION_VALID = "descriptionValid";
	
	
	//PROPERTIES/CONSTANTS
	
	ArrayList<String> properties = new ArrayList<String>(COUNT);
	ArrayList<Object> propertyValues = new ArrayList<Object>(COUNT);
	
	public static final String STATUS_ACTIVE = "statusActive";
	public static final String STATUS_INACTIVE = "statusInActive";
	
	public enum DB_TYPES {
		DB_INTEGER, DB_STRING, DB_DATE, DB_BOOLEAN, DB_KEY, DB_KEY_AUTO
	}
	
	// DB_ID,
	public static final String[] PROPERTIES_LIST = { 
			BEER_ID, COUNTRY_ID, BREWER_ID, STYLE_ID,
			RAW_NAME, NAME, NAME_SUBSCRIPT,
			CONTAINER, BOTTLE, DRAUGHT, CAN,
			DATE_ADDED, SIMPLE_DATE, TASTED,
			DESCRIPTION, CITY, COUNTRY_NAME,
			STYLE_NAME, BREWER_NAME, 
			COMMENTS, RATING, AVERAGE_RATING,			
			INTERNAL_DATE_ADDED, DB_ID , MEMBER_ID, STATUS
			};
	
	public static final DB_TYPES[] TYPES_LIST = { 
		DB_TYPES.DB_INTEGER, DB_TYPES.DB_INTEGER, DB_TYPES.DB_INTEGER, DB_TYPES.DB_INTEGER,
		DB_TYPES.DB_STRING, DB_TYPES.DB_STRING, DB_TYPES.DB_STRING,
		DB_TYPES.DB_STRING, DB_TYPES.DB_BOOLEAN, DB_TYPES.DB_BOOLEAN, DB_TYPES.DB_BOOLEAN,
		DB_TYPES.DB_DATE, DB_TYPES.DB_STRING, DB_TYPES.DB_BOOLEAN,
		DB_TYPES.DB_STRING, DB_TYPES.DB_STRING, DB_TYPES.DB_STRING,
		DB_TYPES.DB_STRING, DB_TYPES.DB_STRING, 
		DB_TYPES.DB_STRING, DB_TYPES.DB_INTEGER, DB_TYPES.DB_INTEGER,			
		DB_TYPES.DB_DATE, DB_TYPES.DB_KEY , DB_TYPES.DB_INTEGER, DB_TYPES.DB_STRING
		};

	HashMap<DB_TYPES, String> typeToSQL = new HashMap<DB_TYPES,String>();

	public MetaBeer() {

		typeToSQL.put(DB_TYPES.DB_INTEGER, "INTEGER");
		typeToSQL.put(DB_TYPES.DB_STRING, "TEXT" );
		typeToSQL.put(DB_TYPES.DB_DATE, "INTEGER");
		typeToSQL.put(DB_TYPES.DB_BOOLEAN, "BOOLEAN");
		typeToSQL.put(DB_TYPES.DB_KEY, "INTEGER PRIMARY KEY");

	}

	public String generateCreateCommand(String tableNameIn) {
		StringBuilder sb = new StringBuilder();
		sb.append("CREATE TABLE " + tableNameIn + " (");
		for (int i = 0; i < PROPERTIES_LIST.length; i++) {
			sb.append(PROPERTIES_LIST[i] + " " + typeToSQL.get(TYPES_LIST[i]));
			if (i != PROPERTIES_LIST.length-1) {
				sb.append(",");
			}
		}
		sb.append(");");
		//Log.d(TAG,"Create command: " + sb.toString());
		return sb.toString();
	}

	public static ContentValues generateTestBeer(int i) {
		ContentValues cb = new ContentValues();
		Date d1 = Calendar.getInstance().getTime();
		//String s1 = String.valueOf(d1.getTime());
		String s2 = i + d1.getHours() + "-" + d1.getMinutes() + "-" + d1.getSeconds();
		String s3 = i + "-" + d1.getSeconds();
		String s4 = "" + d1.getHours() + d1.getMinutes() + d1.getSeconds();
		Integer i2 = Integer.valueOf(s4);
		Integer i3 = Integer.valueOf(s4)+i;
		
		
   		cb.put(MetaBeer.BEER_ID, i2);
		//cb.put(MetaBeer.BREWER_ID, mr.group(4) != null ? Integer.valueOf(mr.group(4)) : null);
		cb.put(MetaBeer.STYLE_ID, i3 );
		//cb.put(MetaBeer.COUNTRY_ID, mr.group(8) != null ? Integer.valueOf(mr.group(8)) : null);
		cb.put(MetaBeer.DESCRIPTION, "DUH1");
		cb.put(MetaBeer.CONTAINER, "bottled");
		//if (mr.group(12).equals("bottled")) {
			cb.put(MetaBeer.BOTTLE, true);
		//} else if (mr.group(12).equals("draught")) {
			cb.put(MetaBeer.DRAUGHT, true);
		//s}
		
		
		//make name, nameSubScript
		//cb.put(Beer2.RAW_NAME, mr.group(14));
		//String rawName = mr.group(14);
		//if (rawName.contains("(")) {
			cb.put(MetaBeer.NAME, "A-" + s2);
			//cb.put(MetaBeer.NAME_SUBSCRIPT, (rawName.substring(rawName.length()-4,rawName.length()-1)));
			//if (cb.getAsString(MetaBeer.NAME_SUBSCRIPT).equals("CAN")) {
			//	cb.put(MetaBeer.CAN, true);
			//}
		//} else {
			//cb.put(MetaBeer.NAME, rawName);
		//}

		//cb.put(MetaBeer.CITY, mr.group(16));
		cb.put(MetaBeer.STYLE_NAME, "A-" + s3);
		//cb.put(MetaBeer.COUNTRY_NAME, mr.group(20));
		//cb.put(MetaBeer.BREWER_NAME, mr.group(22));
		
		//UserBeer fields
		//ub.put(UserBeer.BEER_ID, mr.group(2) != null ? Integer.valueOf(mr.group(2)) : null);
		//String group24 = mr.group(24);	
		//if (mr.group(24) != null && ! group24.equals("") ) {
			Long curDate = d1.getTime();
			cb.put(MetaBeer.DATE_ADDED, curDate);
			cb.put(MetaBeer.SIMPLE_DATE, parseToSimpleDate(curDate));
			if (i == 3 || i==2) {
				cb.put(MetaBeer.TASTED, false);
			} else {
				cb.put(MetaBeer.TASTED, true);
			}
			cb.put(MetaBeer.STATUS, MetaBeer.STATUS_ACTIVE);
		//} else {
		//	cb.put(MetaBeer.TASTED, false);
		//}
		
		//cb.put(MetaBeer.COMMENTS, mr.group(26));
		//cb.put(MetaBeer.RATING, mr.group(28) != null ? Integer.valueOf(mr.group(28)) : null);        		
		
		//Log.d(TAG, "Added beer:" + cb.getAsString(MetaBeer.NAME));
		
		return cb;
	}
	
	public static String parseToSimpleDate(Long in) {
		String ret = "";
		if (in != null) {
			Date d = new Date();
			d.setTime(in);
			ret = sdf2.format(d);
		}
		return ret;
	}
	
	public static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
	public static final String dateFormatSimple = "MM-dd-yyyy";
	public static final SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
	public static final SimpleDateFormat sdf2 = new SimpleDateFormat(dateFormatSimple);
	
	
	
	public static Long parseRawDate(String rawDateIn) {
		Long date = null;
		if (rawDateIn != null) {
			try {
	
				date = sdf.parse(rawDateIn).getTime();

			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		return date;

	}
	
	public static final String dateFormatWebSite = "yyyy-MM-dd";
	public static final SimpleDateFormat sdf3 = new SimpleDateFormat(dateFormatWebSite);

	
	public static Long parseRawDateWebsite(String rawDateIn) {
		Long date = null;
		if (rawDateIn != null) {
			try {
	
				date = sdf3.parse(rawDateIn).getTime();

			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		return date;

	}


}
